package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.SystemData;
import com.tbit.uqbike.client.dao.core.RideLogDao;
import com.tbit.uqbike.client.pojo.Machine;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.RideLog;
import com.tbit.uqbike.client.pojo.RideLogDetails;
import com.tbit.uqbike.client.service.RideLogDetailsService;
import com.tbit.uqbike.client.service.RideLogService;
import com.tbit.uqbike.client.util.Assert;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("rideLogService")
public class RideLogServiceImpl implements RideLogService {
    @Autowired
    private RideLogDao rideLogDao;
    @Autowired
    private RideLogDetailsService rideLogDetailsService;

    @Override
    public void insert(RideLog rideLog) {
        rideLogDao.insert(rideLog);
    }

    @Override
    public void update(RideLog rideLog) {
        rideLogDao.update(rideLog);

    }

    @Override
    public List<RideLog> getByUserId(Integer userId) {
        List<RideLog> rideLogs = rideLogDao.getByUserId(userId);
        if (rideLogs.size() > 0) {
            for (RideLog ridelog : rideLogs) {
                Machine machine = SystemData.machineInfoMap.get("machineId." + ridelog.getMachineId());
                if (machine != null) {
                    ridelog.setMachineType(machine.getMachineType());
                }
            }
        }
        return rideLogs;
    }

    @Override
    public Integer queryCount(Integer userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);

        return rideLogDao.queryCount(params);
    }

    @Override
    public List<RideLog> queryPage(Integer userId, Integer rowCount, Integer pageNO) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("start", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);

        List<RideLog> rideLogs = rideLogDao.queryPage(params);
        //430版本二期需求，要把订单详情字段里的停车时长、停车费用去掉
        this.removeStopTime(rideLogs);
        rideLogs.forEach(RideLog::parsePayStatus);
        return rideLogs;
    }

    @Override
    public RideLog getByUserIdNotFinish(Integer userId) {
        return rideLogDao.getByUserIdNotFinish(userId);
    }

    @Override
    public void updateStopTime(Integer userId, Integer stopTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("stopTime", stopTime);

        rideLogDao.updateStopTime(params);
    }

    @Override
    public void updateByOrderNO(RideLog rideLog) {
        rideLogDao.updateByOrderNO(rideLog);
    }

    @Override
    public void updateStopTimeByOrderNO(String orderNO, Integer stopTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("orderNO", orderNO);
        params.put("stopTime", stopTime);

        rideLogDao.updateStopTimeByOrderNO(params);
    }

    @Override
    public RideLog getByOrderNO(String orderNO) {
        return rideLogDao.getByOrderNO(orderNO);
    }

    @Override
    public RideLog findNOtPayByUserId(Integer userId) {
        return rideLogDao.findNOtPayByUserId(userId);
    }

    @Override
    public RideLog findFinishButNOtPayByUserId(Integer userId) {
        return rideLogDao.findFinishButNOtPayByUserId(userId);
    }

    @Override
    public void updatePayTimeByUserIdAndOrderNO(Integer userId, Date payTime, String orderNO) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("payTime", payTime);
        map.put("orderNO", orderNO);
        rideLogDao.updatePayTimeByUserIdAndOrderNO(map);
    }

    @Override
    public RideLog findLastByUserId(Integer userId) {
        return rideLogDao.findLastByUserId(userId);
    }

    @Override
    public void updateByOrderNOSelective(RideLog rideLog) {
        rideLogDao.updateByOrderNOSelective(rideLog);
    }

    @Override
    public void updateWxzff(RideLog rideLog) {
        rideLogDao.updateWxzff(rideLog);
    }

    @Override
    public void editRegionId(RideLog rideLog) {
        rideLogDao.editRegionId(rideLog);
    }

    @Override
    public void addBatch(List<RideLog> list) {
        rideLogDao.addBatch(list);
    }

    @Override
    public Integer getTodayNo(Integer userId, String startTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("startTime", startTime);
        return rideLogDao.getTodayNo(map);
    }

    @Override
    public Integer getRideCount(Integer userId,Integer days) {
        Assert.notNull(userId,"-30001","userId参数不能为空");
        Assert.notNull(days,"-30001","isPay参数不能为空");
        return rideLogDao.getRideCount(userId,days);
    }

    @Override
    public RideLog getLastByUserId(Integer userId) {
        return rideLogDao.findLastByUserId(userId);
    }



    /**
     * @description: 批量去除掉订单详情里面的停车时长、停车费用
     * @author: mo.shanyong
     * @date: 2024/12/10 17:12
     * @param: rideLogs
     */
    private void removeStopTime(List<RideLog> rideLogs){
        if (CollectionUtils.isEmpty(rideLogs))
            return;
        for (RideLog rideLog : rideLogs) {
            this.removeStopParam(rideLog);
        }
    }

    /**
     * @description: 单个去除掉订单详情里面的停车时长、停车费用
     * @author: mo.shanyong
     * @date: 2024/12/10 17:12
     * @param:
     */
    private void removeStopParam(RideLog rideLog){
        if (StringUtils.isBlank(rideLog.getRemark()))
            return;
        String[] split = rideLog.getRemark().split("[,，]");
        List<String> list = new ArrayList<>();
        //标志位，看看是否存在停车时长
        boolean haveStopTime = false;
        for (String s : split) {
            //去除掉停车时长
            if (s.contains("停车时长") || s.contains("停车费用")){
                haveStopTime = true;
                continue;
            }
            list.add(s);
        }
        //没有停车时长，不需要替换订单详情，直接跳下一个
        if (!haveStopTime){
            return;
        }
        rideLog.setRemark(String.join(",",list));
    }

    /**
     * 订单详情添加支付状态
     *
     * @param rideLogs 订单详情集合
     */
    @Deprecated
    private void addPayStatus(List<RideLog> rideLogs) {
        //后续一定要避免做for循环查大表的操作
        for (RideLog rideLog : rideLogs) {
            Result<RideLogDetails> rideLogDetailsResult = rideLogDetailsService.queryByOrderId(rideLog.getOrderNO());
            rideLog.setPayStatus(rideLogDetailsResult.getData().getPayStatus());
            rideLog.setDeductionEndMoney(rideLogDetailsResult.getData().getDeductionEndMoney());
        }
    }
}